package com.demo.controller;

import com.demo.common.permission.PermissionCheck;
import com.demo.common.permission.PermissionKey;
import com.demo.common.vo.Result;
import com.demo.common.vo.SimpleKVVo;
import com.demo.entity.vo.SysRolePermissionSaveReq;
import com.demo.service.SysRolePermissionService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色权限controller
 */
@RestController
@RequestMapping("/sys/role/permission")
public class SysRolePermissionController {

    @Resource
    private SysRolePermissionService sysRolePermissionService;

    /**
     * 更新角色权限
     * @param sysRolePermissionSaveReq
     * @return
     */
    @PostMapping("/update")
    @PermissionCheck(PermissionKey.VIEW_ROLE_MANAGER)
    public Result add(@RequestBody @Valid SysRolePermissionSaveReq sysRolePermissionSaveReq){
        sysRolePermissionService.saveRolePermission(sysRolePermissionSaveReq);
        return Result.success();
    }

    /**
     * 获取角色权限
     * @param roleId
     * @return
     */
    @GetMapping("/detail")
    @PermissionCheck(PermissionKey.VIEW_ROLE_MANAGER)
    public Result detail(@RequestParam("roleId") Integer roleId){
        return Result.success(sysRolePermissionService.getRolePermission(roleId));
    }

    /**
     * 获取所有权限
     * @return
     */
    @GetMapping("/list/all")
    public Result allPermission(){
        List<SimpleKVVo> list = PermissionKey.getPermissionMap().entrySet().stream().map(it -> new SimpleKVVo(it.getKey(), it.getValue())).collect(Collectors.toList());
        return Result.success(list);
    }
}
